package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2023-11-15
 * Time: 22:20
 */
//删除图书
public class DelOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("删除图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除图书的书名:");
        String name = scanner.nextLine();

        int index = -1;
        int currenSize = bookList.getUsedSize();
        int i = 0;
        for (; i < currenSize; i++) {//遍历存放图书的数组
            Book tmp = bookList.getBooks(i);
            if (tmp.getName().equals(name)) {
                index = i;   //找到下标,记录下来
                break;
            }
        }
        //
        if( i>=currenSize){
            System.out.println("没有找到要删除的图书");
            return;
        }
        //可以删了
        for (int j = index;j<currenSize-1 ; j++){
            Book book = bookList.getBooks(j+1);//拿到所要删除(在j位置)的后一个书(j+1)
            bookList.setBooks(book,j);//把上面拿到的(j+1)的书放在j位置
        }

        bookList.setBooks(null,currenSize-1);//手动置为空
        bookList.setUsedSize(currenSize-1);
        System.out.println("删除成功!");
    }
}
